package com.photoeditor.demo.ui.widget.image.hair;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.util.ImageRectUtils;
import com.photoeditor.demo.util.TouchUtil;


/**
 * 自定义的SeekBar 中间有数字的seekbar
 * 注意 这个View不支持设置Padding
 */
public class CustomSizeSeekBar extends View {

    private final int MIN_HEIGHT;

    private final int MAX_HEIGHT;

    /**
     * 进度的最大值
     */
    private int mMax;

    /**
     * 当前的进度
     */
    private int mProgress;


    /**
     * 是否初始化完成
     */
    private boolean mIsInit = false;


    private RectF mViewRect;

    /**
     * 下面用于处理Touch
     */
    private float mDownX;
    private final int TYPE_MOVE = 2;
    private final int TYPE_DOWN = 1;
    private final int TYPE_NONE = 0;
    private int mTouchType = TYPE_NONE;

    private OnSeekBarChangeListener mListener;

    private Path mBgPath;
    private Paint mBgPathPaint;

    private Path mVisiblePath;
    private Paint mVisiblePathPaint;

    private Paint mLargePaint;
    private Paint mLittlePaint;

    private RectF mLargeCircleRect;
    private RectF mLittleCircleRect;

    private final int LARGE_CIRCLE_DOUBLE_RADIUS;

    public CustomSizeSeekBar(Context context) {
        this(context, null);
    }

    public CustomSizeSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        MIN_HEIGHT = DeviceUtils.dip2px(ComponentContext.getContext(), 1);
        MAX_HEIGHT = DeviceUtils.dip2px(ComponentContext.getContext(), 24);
        LARGE_CIRCLE_DOUBLE_RADIUS = getResources().getDimensionPixelSize(R.dimen.image_edit_hair_size_seekbar);
    }

    /**
     * 初始化
     */
    private void init() {
        setWillNotDraw(false);
        mBgPath = new Path();
        mVisiblePath = new Path();
        mBgPathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPathPaint.setStyle(Paint.Style.FILL);
        //mBgPathPaint.setColor(Color.parseColor("#4cffffff"));
        mBgPathPaint.setColor(ComponentContext.getContext().getResources().getColor(R.color.size_seek_bar_bg));

        mVisiblePathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mVisiblePathPaint.setStyle(Paint.Style.FILL);
        //mVisiblePathPaint.setColor(Color.parseColor("#ffffff"));
        mVisiblePathPaint.setColor(ComponentContext.getContext().getResources().getColor(R.color.size_seek_bar_bg_visible));

        mLargePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLargePaint.setStyle(Paint.Style.FILL);
        //mLargePaint.setColor(Color.parseColor("#b2333333"));
        mLargePaint.setColor(ComponentContext.getContext().getResources().getColor(R.color.size_seek_bar_circle_bg));

        mLittlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLittlePaint.setStyle(Paint.Style.FILL);
        //mLittlePaint.setColor(Color.parseColor("#ffffff"));
        mLittlePaint.setColor(ComponentContext.getContext().getResources().getColor(R.color.size_seek_bar_circle));

        mLargeCircleRect = new RectF();
        mLittleCircleRect = new RectF();

        mMax = 100;
//		mProgress = 50;
    }

    /**
     * 做初始化的动作
     */
    private void doInit() {
        if (!mIsInit) {
            mIsInit = true;
            mViewRect = ImageRectUtils.getViewRect(this);
            mViewRect.offset(-mViewRect.left, -mViewRect.top);
            mViewRect.set(mViewRect.left + LARGE_CIRCLE_DOUBLE_RADIUS / 2, mViewRect.top, mViewRect.right - LARGE_CIRCLE_DOUBLE_RADIUS / 2, mViewRect.bottom);

            float left_center_x = mViewRect.left;
            float left_center_y = mViewRect.centerY();

            float start_x = left_center_x + MIN_HEIGHT / 2;
            float start_y = left_center_y - MIN_HEIGHT / 2;

            float right_center_x = mViewRect.right;
            float right_center_y = mViewRect.centerY();

            float second_x = right_center_x - MAX_HEIGHT / 2;
            float second_y = right_center_y - MAX_HEIGHT / 2;

            float third_x = start_x;
            float third_y = start_y + MIN_HEIGHT;

            mBgPath.reset();
            mBgPath.moveTo(start_x, start_y);
            mBgPath.lineTo(second_x, second_y);
            mBgPath.lineTo(second_x, second_y + MAX_HEIGHT);
            RectF rectF1 = new RectF(right_center_x - MAX_HEIGHT, right_center_y - MAX_HEIGHT / 2, right_center_x, right_center_y + MAX_HEIGHT / 2);
            mBgPath.addArc(rectF1, -90, 180);//最大圆
            mBgPath.lineTo(third_x, third_y);
            mBgPath.lineTo(start_x, start_y);
            RectF rectF2 = new RectF(new RectF(left_center_x, left_center_y - MIN_HEIGHT / 2, left_center_x + MIN_HEIGHT, left_center_y + MIN_HEIGHT / 2));
            mBgPath.addArc(rectF2, 270, -180);//最小圆

            setDrawInfo(mProgress, mMax);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            doInit();
        }
    }

    /**
     * 重写onMeasure确保能完全显示 默认30dp
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = getResources().getDimensionPixelSize(R.dimen.image_edit_hair_size_seekbar);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
                MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mIsInit) {
            canvas.drawPath(mBgPath, mBgPathPaint);

            canvas.drawPath(mVisiblePath, mVisiblePathPaint);

            canvas.drawOval(mLargeCircleRect, mLargePaint);

            canvas.drawOval(mLittleCircleRect, mLittlePaint);
        }
    }


    /**
     * 设置最大的进度值
     *
     * @param max
     */
    public void setMax(int max) {
        mMax = max;
        setDrawInfo(mProgress, mMax);
    }

    /**
     * 外部设置进度  回调时会返回isFromUser 为false
     *
     * @param progress
     */
    public void setProgress(int progress) {
        mProgress = Math.min(progress, mMax);
        mProgress = Math.max(0, mProgress);
        setDrawInfo(mProgress, mMax);
        if (mListener != null) {
            mListener.onProgressChanged(this, mProgress, false);
        }
    }

    /**
     * false
     * touch的时候设置进度  回调时会返回isFromUser 为true
     *
     * @param progress
     */
    private void setProgressFromTouch(int progress) {
        mProgress = Math.min(progress, mMax);
        mProgress = Math.max(0, mProgress);
        setDrawInfo(mProgress, mMax);
        if (mListener != null) {
            mListener.onProgressChanged(this, mProgress, true);
        }
    }

    /**
     * 设置绘制的信息
     */
    private void setDrawInfo(int progress, int max) {
        if (!mIsInit) return;
        float maxWidth = mViewRect.width() - MIN_HEIGHT / 2 - MAX_HEIGHT / 2;
        float percent = progress * 1.0f / max;

        float curWidth = maxWidth * percent;
        float curDoubleRadius = percent * (MAX_HEIGHT - MIN_HEIGHT) + MIN_HEIGHT;//当前的直径

        mVisiblePath.reset();
        float left_center_x = mViewRect.left;
        float left_center_y = mViewRect.centerY();

        float start_x = left_center_x + MIN_HEIGHT / 2;
        float start_y = left_center_y - MIN_HEIGHT / 2;

        float right_center_x = start_x + curWidth + curDoubleRadius / 2;//圆心 + 距离 + 圆心
        float right_center_y = mViewRect.centerY();

        float second_x = right_center_x - curDoubleRadius / 2;
        float second_y = right_center_y - curDoubleRadius / 2;

        float third_x = start_x;
        float third_y = start_y + MIN_HEIGHT;

        mVisiblePath.reset();
        mVisiblePath.moveTo(start_x, start_y);
        mVisiblePath.lineTo(second_x, second_y);
        mVisiblePath.lineTo(second_x, second_y + curDoubleRadius);
        RectF rectF1 = new RectF(right_center_x - curDoubleRadius, right_center_y - curDoubleRadius / 2, right_center_x, right_center_y + curDoubleRadius / 2);
        mVisiblePath.addArc(rectF1, -90, 180);//最大圆
        mVisiblePath.lineTo(third_x, third_y);
        mVisiblePath.lineTo(start_x, start_y);
        RectF rectF2 = new RectF(new RectF(left_center_x, left_center_y - MIN_HEIGHT / 2, left_center_x + MIN_HEIGHT, left_center_y + MIN_HEIGHT / 2));
        mVisiblePath.addArc(rectF2, 270, -180);//最小圆

        mLittleCircleRect.set(right_center_x - curDoubleRadius, right_center_y - curDoubleRadius / 2, right_center_x, right_center_y + curDoubleRadius / 2);

        float circleCenterX = mLittleCircleRect.centerX();
        float circleCenterY = mLittleCircleRect.centerY();
        float largeRadius = LARGE_CIRCLE_DOUBLE_RADIUS / 2;
        mLargeCircleRect.set(circleCenterX - largeRadius, circleCenterY - largeRadius, circleCenterX + largeRadius, circleCenterY + largeRadius);
        invalidate();
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener listener) {
        mListener = listener;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mDownX = event.getX();
            if (mLargeCircleRect.contains((int) mDownX, (int) (event.getY()))) {//点在区域内
                mTouchType = TYPE_DOWN;
            } else {
                if (mListener != null) {
                    mListener.onStartTrackingTouch(this);
                }
                int progress = (int) (mDownX * 1.0f / mViewRect.width() * mMax + 0.5f);
                setProgressFromTouch(progress);
                mTouchType = TYPE_MOVE;
            }
            invalidate();
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            float fdownX = event.getX();
            if (mTouchType == TYPE_DOWN) {
                if (Math.abs(fdownX - mDownX) >= TouchUtil.OFFSET) {
                    if (mListener != null) {
                        mListener.onStartTrackingTouch(this);
                    }
                    int progress = (int) (fdownX * 1.0f / mViewRect.width() * mMax + 0.5f);
                    setProgressFromTouch(progress);
                    mTouchType = TYPE_MOVE;
                }
            } else if (mTouchType == TYPE_MOVE) {
                int progress = (int) (fdownX * 1.0f / mViewRect.width() * mMax + 0.5f);
                setProgressFromTouch(progress);
            }
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
            mTouchType = TYPE_NONE;
            if (mListener != null) {
                mListener.onStopTrackingTouch(this);
            }
            invalidate();
        }
        return super.onTouchEvent(event);
    }


    public int getProgress() {
        return mProgress;
    }

    /**
     * 获取当前的最大进度值
     *
     * @return
     */
    public int getMax() {
        return mMax;
    }

}
